public class MyLinkedList implements IList{

    //内部类
    public class ListNode {
        public int val;
        public ListNode prev;//前驱
        public ListNode next;//后置

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;
    //头插法
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        //判断是否为空
        if(head == null) {
            head = node;
            last = node;
        }else {//不为空情况
            //先尾巴
            node.next = head;
            //后前驱
            head.prev = node;
            head = node;
        }
    }
    //尾插法
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(head == null) {
            head = node;
            last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data) {
        //判断index是否符合条件 不符合条件返回一个错误
        if(index < 0 || index > size()) {
            throw new IndexException();
        }
        //0位置 头插
        if(index == 0) {
            addFirst(data);
            return;
        }
        //size()位置 尾插
        if(index == size()) {
            addLast(data);
            return;
        }
        //中间位置 先找到插入节点 再进行插入
        ListNode cur = findList(index);
        ListNode node = new ListNode(data);
        //插入
        //先尾
        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;
        //后先驱
    }
    private ListNode findList(int index) {
        ListNode cur = head;
        while(index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        //寻找key节点
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                //头节点
                if(cur == head) {//防止前驱空指针异常
                    head= head.next;
                    if(head != null) {//多个节点时
                        head.prev = null;//如果只有一个节点时，head为空后 没有head.prev
                    }else {//只有一个节点时
                        last = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    //尾节点 防止后置节点为空
                    if(cur.next == null) {
                        //cur.prev.next = cur.next;
                        last = last.prev;
                    }else {
                        //中间节点
                        cur.next.prev = cur.prev;
                        //cur.prev.next = cur.next;
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {
        ListNode cur = head;
        while(cur != null) {
            if(cur.val == key) {
                //头节点
                if(cur == head) {//防止前驱空指针异常
                    head= head.next;
                    if(head != null) {//多个节点时
                        head.prev = null;//如果只有一个节点时，head为空后 没有head.prev
                    }else {//只有一个节点时
                        last = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    //尾节点 防止后置节点为空
                    if(cur.next == null) {
                        //cur.prev.next = cur.next;
                        last = last.prev;
                    }else {
                        //中间节点
                        cur.next.prev = cur.prev;
                        //cur.prev.next = cur.next;
                    }
                }
            }
            cur = cur.next;
        }
    }
    //得到单链表的长度
    public int size() {
        ListNode cur = head;
        int count = 0;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void display() {
        ListNode cur = head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
    public void clear() {
        this.head = null;
        this.last = null;
    }
}
